package com.gexiaobao.pigeon.ui.fragment.mall.store

import android.os.Bundle
import androidx.core.view.isVisible
import androidx.fragment.app.setFragmentResultListener
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.model.bean.StoreInfoResponse
import com.gexiaobao.pigeon.app.util.ImageLoaderManager
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.databinding.FragmentMineStoreBinding
import com.gexiaobao.pigeon.viewmodel.MerchantStoresViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/7/11 14:05
 * @Describe    : 我的店铺
 */
class FragmentMineStore : BaseFragment<MerchantStoresViewModel, FragmentMineStoreBinding>() {

    private lateinit var storeInfoResponse: StoreInfoResponse

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.tvWhiteTitle.text = getString(R.string.my_shop)
        mDatabind.includeBar.tvRedBarRightMenu.text = getString(R.string.copyreader)
        mDatabind.includeBar.tvRedBarRightMenu.isVisible = true

        setFragmentResultListener("isOpen") { _, bundle ->
            if (bundle.getInt("isOpen") == 1) {
                lazyLoadData()
            }
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        showLoadingAntiShake(getString(R.string.loading))
        mViewModel.getAppShopInfo()
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.mineStoreInfoResult.observe(viewLifecycleOwner) {
            dismissLoadingAntiShake()
            if (it.shopName.isEmpty()) {
                mDatabind.includeBar.tvRedBarRightMenu.isVisible = false
            }
            ImageLoaderManager.loadCircleImageFall(context, it.shopPicUrl, mDatabind.ivMineShopLogo, R.mipmap.icon_upload_shop_logo)
            mDatabind.data = it
            storeInfoResponse = it
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvOpenStore, mDatabind.includeBar.tvRedBarRightMenu, mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> {
                    nav().navigateUp()
                }

                mDatabind.tvOpenStore -> {
                    nav().navigateAction(R.id.action_to_fragment_open_store, Bundle().apply {
                        putString(FragmentOpenStore.intentType, "1")
                    })
                }

                mDatabind.includeBar.tvRedBarRightMenu -> {
                    if (::storeInfoResponse.isInitialized) {
                        nav().navigateAction(R.id.action_to_fragment_open_store, Bundle().apply {
                            putString(FragmentOpenStore.intentType, "2")
                            putParcelable(FragmentOpenStore.data, storeInfoResponse)
                        })
                    } else {
                        RxToast.showToast(getString(R.string.get_store_info_fail))
                    }
                }
            }
        }
    }
}